﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;
using Runda.EShop.Common.Utility;
using System.Data;
using Runda.EShop.Common.Component;

namespace Runda.EShop.SaaS.OrderManage
{
    public partial class EmptyOrderPendingGen : BasePage
    {
        public Guid OrderID
        {
            get
            {
                if (Request.QueryString["OrderID"] != null)
                {
                    return new Guid(Request.QueryString["OrderID"]);
                }
                return Guid.Empty;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initialize();
            }
        }

        private void Initialize()
        {
            OrderBll bll = new OrderBll();
            BizOrderEntity entity = bll.Get<BizOrderEntity>(OrderID);
            ucOrderInfo.OrderEntity = entity; 

            GetOrderDetail(); 
        }

        private void GetOrderDetail()
        {
            if (OrderID == Guid.Empty) return;
            BizOrderDetailBll bll = new BizOrderDetailBll();
            DataTable dt = bll.GetOrderDetail(OrderID);
            gvOrderDetail.DataSource = dt;
            gvOrderDetail.DataBind();
        }

        protected void gvOrderDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dv = e.Row.DataItem as DataRowView;
                int FollowResult = 0;
                if (dv["FollowResult"] != DBNull.Value)
                {
                    FollowResult = Convert.ToInt32(dv["FollowResult"].ToString());
                }

                if (FollowResult == 1)
                {
                    CheckBox ckbSelect = e.Row.FindControl("ckbSelect") as CheckBox;
                    ckbSelect.Visible = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<Guid> list = new List<Guid>();
            foreach (GridViewRow row in gvOrderDetail.Rows)
            {
                CheckBox ckbSelect = row.FindControl("ckbSelect") as CheckBox;
                if (ckbSelect.Checked)
                {
                    Guid DetailID = new Guid(gvOrderDetail.DataKeys[row.RowIndex]["DetailID"].ToString());

                    list.Add(DetailID);
                }
            }

            //后台再验证，以防恶意程序绕过前端验证
            if (list.Count == 0)
            {
                return;
            }

            OrderBll bll = new OrderBll();
            bll.GenerateOrderByEmptyOrder(OrderID, list,base.User.Identity.LoginUser.UserID);
            ClientScript.RegisterStartupScript(this.GetType(), "asdf", "alert('保存成功.');window.close();", true);
        }
    }
}